﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.ModelBinding;
using OUSMS.AppLogic.BusinessLogicLayer;
using OUSMS.AppLogic.DataAccessLayer;
using OUSMS.Helpers;

namespace OUSMS
{
    public partial class UpdateProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UsersBLL usersBLL = new UsersBLL();
                Users userEntity = usersBLL.GetUserByLoginName(User.Identity.Name);
                loginNameTxtBox.Text = User.Identity.Name;
                nameTxtBox.Text = userEntity.Name;
                emailTxtBox.Text = userEntity.Email;

                departmentDDL.DataSource = GetAllDepartments().ToList();
                departmentDDL.DataBind();

                SecondDepartmentDDL.DataSource = GetSecondDepartmentsByDepartmentID(Convert.ToInt32(departmentDDL.SelectedValue)).ToList();
                SecondDepartmentDDL.DataBind();
            }  
        }

        public IQueryable<Titles> GetAllTitles()
        {
            TitlesBLL titlesBLL = new TitlesBLL();
            return titlesBLL.GetAllTitles();
        }

        public IQueryable<StudyAreas> GetALLStuyAreas()
        {
            StudyAreasBLL studyAreaBLL = new StudyAreasBLL();
            return studyAreaBLL.GetALLStuyAreas();
        }

        public IQueryable<Departments> GetAllDepartments()
        {
            DepartmentsBLL departmentsBLL = new DepartmentsBLL();
            return departmentsBLL.GetAllDepartments();
        }

        public IQueryable<SecondDepartments> GetSecondDepartmentsByDepartmentID(int departmentID)
        {
            SecondDepartmentsBLL secondDepartmentBLL = new SecondDepartmentsBLL();
            return secondDepartmentBLL.GetSecondDepartmentsByDepartmentID(departmentID);
        }

        protected void departmentDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            SecondDepartmentDDL.DataSource = GetSecondDepartmentsByDepartmentID(Convert.ToInt32(departmentDDL.SelectedValue)).ToList();
            SecondDepartmentDDL.DataBind();
        }

        protected void submitBtn_Click(object sender, EventArgs e)
        {
            try
            {
                UsersBLL usersBLL = new UsersBLL();
                Users userEntity = usersBLL.GetUserByLoginName(User.Identity.Name);
                userEntity.Name = nameTxtBox.Text.Trim();
                userEntity.Gender = genderDDL.SelectedValue;
                userEntity.Email = emailTxtBox.Text.Trim();
                userEntity.Phone = phoneTxtBox.Text.Trim();
                userEntity.DepartmentID = Convert.ToInt32(departmentDDL.SelectedValue);
                userEntity.SecondDepartmentID = Convert.ToInt32(SecondDepartmentDDL.SelectedValue);
                userEntity.Position = positionTxtBox.Text.Trim();
                userEntity.TitleID = Convert.ToInt32(titleDDL.SelectedValue);
                userEntity.Introduction = introductionTxtBox.Text.Trim();
                usersBLL.Save();

                for (int index = 0; index < studyAreaChkList.Items.Count; index++)
                {
                    usersBLL.ClearUserStudyAreas(userEntity);
                    if (studyAreaChkList.Items[index].Selected)
                    {
                        UserStudyAreas userStudyArea = new UserStudyAreas();
                        userStudyArea.StudyAreaID = Convert.ToInt32(studyAreaChkList.Items[0].Value);
                        userStudyArea.UserID = userEntity.ID;
                        usersBLL.AddUserStudyArea(userStudyArea);
                    }
                }
                Response.Redirect("WorkCenter.aspx");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                errorMsgLbl.Visible = true;
                errorMsgLbl.Text = string.Format("信息提交失败，请根据下列原因进行修改或者将错误信息报告管理员：", ex.Message);
            }
        }
    }
}